import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 *  退出登录的后端代码.
 */
@WebServlet("/signOut")
public class SignOut extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取 session
        HttpSession session = req.getSession(false);
        if (session == null) {
            resp.setStatus(403);
        } else {
            // 直接把 session 的 user 对象删掉, 返回一个重定向.
            session.removeAttribute("user");
            resp.setStatus(302);
            resp.sendRedirect("login.html");
        }
    }
}
